﻿using Blog.Entity.Domain;
using Blog.EntityFrameWork.EntityFramework.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Blog.Core.Entities
{
    public abstract class EntitiesDomainService<TEntity, TPrimaryKey> : IEntitiesDomainService<TEntity, TPrimaryKey> where TEntity : Entity<TPrimaryKey>
    {
        private readonly IRepository<TEntity, TPrimaryKey> _repository;

        public EntitiesDomainService(IRepository<TEntity, TPrimaryKey> repository)
        {
            _repository = repository;
        }


        public virtual async Task<TEntity> FirstOrDefultAsync(TPrimaryKey id)
        {
             return await _repository.FirstOrDefultAsync(id);
        }

        public virtual async Task<TEntity> FirstOrDefultAsync(Expression<Func<TEntity, bool>> query)
        {
            return await _repository.FirstOrDefultAsync(query);
        }

        public virtual async Task<List<TEntity>> GetAllListAsync()
        {
            return await _repository.GetAllListAsync();
        }

        public virtual async Task<TPrimaryKey> CreateAndGetIdAsync(TEntity entity)
        {
            return await _repository.CreateAndGetIdAsync(entity);
        }

        public virtual async Task Remove(TEntity entity)
        {
            await _repository.Remove(entity);
        }

        public virtual async Task Remove(Expression<Func<TEntity,bool>> expression)
        {
            await _repository.Remove(expression);
        }

        public virtual async Task<TEntity> UpdateAsync(TEntity entity)
        {
            return await _repository.UpdateAsync(entity);
        }

        public virtual async Task<TEntity> UpdateAsync(TPrimaryKey key,Func<TEntity,Task> func)
        {
            return await _repository.UpdateAsync(key, func);
        }

    }
}
